package tempData.TempDataImpl;

import tempData.BaseTempActData;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class TempData {
    private final Map<Class<? extends BaseTempActData>, BaseTempActData> baseActDataMap = new HashMap<Class<? extends BaseTempActData>, BaseTempActData>();

    public <T extends BaseTempActData> T getActData(Class<T> cls) {
        if (baseActDataMap.containsKey(cls)) {
            return (T) baseActDataMap.get(cls);
        } else {
            try {
                T data = cls.newInstance();
                baseActDataMap.put(cls, data);
                return data;
            } catch (Exception e) {

            }
            return null;
        }
    }

    public void clearByFinish() {
        Iterator<BaseTempActData> iterator = baseActDataMap.values().iterator();
        while (iterator.hasNext()) {
            BaseTempActData baseActData = iterator.next();
            baseActData.clearByFinish();
        }
    }
}
